from django.db import models
from django.contrib.auth.models import User

class MCPServer(models.Model):
    """MCP服务器模型"""
    name = models.CharField(max_length=100, verbose_name="服务器名称")
    description = models.TextField(blank=True, null=True, verbose_name="描述")
    status = models.BooleanField(default=True, verbose_name="状态")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="servers", verbose_name="所有者")
    
    # 简化后保留的扩展字段
    category = models.CharField(max_length=100, default='other', verbose_name="分类")
    category_name = models.CharField(max_length=255, default='其他', verbose_name="分类名称")
    tags = models.JSONField(default=list, verbose_name="标签")
    
    class Meta:
        verbose_name = "MCP服务器"
        verbose_name_plural = "MCP服务器"
        ordering = ['-created_at']
        
    def __str__(self):
        return self.name

class MCPServerFromReadme(models.Model):
    """从README解析的MCP服务器信息"""
    id = models.CharField(max_length=255, primary_key=True, verbose_name="ID")
    name = models.CharField(max_length=255, verbose_name="名称")
    provider = models.CharField(max_length=255, verbose_name="提供者")
    description = models.TextField(blank=True, null=True, verbose_name="描述")
    url = models.URLField(blank=True, null=True, verbose_name="URL")
    github = models.URLField(blank=True, null=True, verbose_name="GitHub地址")
    is_official = models.BooleanField(default=False, verbose_name="是否官方")
    category = models.CharField(max_length=100, verbose_name="分类")
    category_name = models.CharField(max_length=255, verbose_name="分类名称")
    tags = models.JSONField(default=list, verbose_name="标签")
    language = models.JSONField(default=list, verbose_name="语言")
    scope = models.JSONField(default=list, verbose_name="范围")
    os = models.JSONField(default=list, verbose_name="操作系统")
    readme_data = models.JSONField(default=dict, verbose_name="原始数据")
    
    class Meta:
        verbose_name = "README解析的MCP服务器"
        verbose_name_plural = "README解析的MCP服务器"
        ordering = ['category', 'name']
        
    def __str__(self):
        return f"{self.name} ({self.provider})"

class MCPServerTag(models.Model):
    """MCP服务器标签"""
    server = models.ForeignKey(MCPServerFromReadme, on_delete=models.CASCADE, related_name="server_tags", verbose_name="服务器")
    tag = models.CharField(max_length=100, verbose_name="标签")
    
    class Meta:
        verbose_name = "MCP服务器标签"
        verbose_name_plural = "MCP服务器标签"
        unique_together = ('server', 'tag')
        
    def __str__(self):
        return f"{self.server.name} - {self.tag}"
